/**@FILE:ToyServiceImpl.java
 * @AUTHOR:qingsheng.zheng
 * @DATE:2015-1-31 下午6:30:47*/
package com.xf.toy.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.xf.toy.bean.Toy;
import com.xf.toy.bean.ToyExample;
import com.xf.toy.bean.ToyExample.Criteria;
import com.xf.toy.dao.ToyMapper;
import com.xf.toy.dao.page.Pagination;
import com.xf.toy.service.ToyService;

/*******************************************
 * @COMPANY:alibaba
 * @CLASS:ToyServiceImpl
 * @DESCRIPTION:
 * @AUTHOR:qingsheng.zheng
 * @VERSION:v1.0
 * @DATE:2015-1-31 下午6:30:47
 *******************************************/
@Service("toyService")
public class ToyServiceImpl implements ToyService {
	@Resource
	private ToyMapper toyMapper;

	@Override
	public List<Toy> selectToysPagination(Toy toy, Pagination page) {
		ToyExample toyExample = new ToyExample();
		Criteria criteria = toyExample.createCriteria();
		if (StringUtils.isNotEmpty(toy.getToyTheme()))
			criteria.andToyThemeEqualTo(toy.getToyTheme());
		if (StringUtils.isNotEmpty(toy.getToyAge()))
			criteria.andToyAgeEqualTo(toy.getToyAge());
		toyExample.setLimitStart(page.getStart());
		toyExample.setLimitEnd(page.getPageSize());
		page.setTotalcount(toyMapper.countByExample(toyExample));
		return toyMapper.selectByExample(toyExample);
	}

	@Override
	public Toy selectOneByCode(String toycode) {
		return toyMapper.selectByPrimaryKey(toycode);
	}

	@Override
	public void toyBack(String toycode, Long count) {
		Toy toy = this.selectOneByCode(toycode);
		Toy temp = new Toy();
		temp.setToyCode(toycode);
		temp.setToysetsCount(toy.getToysetsCount() + count);
		toyMapper.updateByPrimaryKeySelective(temp);
	}

	public ToyMapper getToyMapper() {
		return toyMapper;
	}

	public void setToyMapper(ToyMapper toyMapper) {
		this.toyMapper = toyMapper;
	}

	@Override
	public List<String> getDistinctAge() {
		return toyMapper.getDistinctAge();
	}

	@Override
	public List<String> getDistinctTheme() {
		return toyMapper.getDistinctTheme();
	}

}
